import GameController from "../GameController";
import { EventManager } from "../engine/EventManager";
import { GameSocket } from "../engine/GameSocket";
import { Logger } from "../engine/Logger";
import ServerTimer from "../engine/ServerTimer";
import UIManager from "../engine/UIManager";
import VersionManager from "../engine/VersionManager";
import { ErrorCode } from "../game/config/ErrorCode";
import { EventConfig } from "../game/config/EventConfig";
import { ViewConfig } from "../game/config/ViewConfig";
import { AccountModel } from "../game/model/AccountModel";
import { UserModel } from "../game/model/UserModel";
import { UIHelper } from "../game/util/UIHelper";
import { ProtoManager, WsSendData } from "../lib/net/protobuf/ProtoManager";
import jhaoproto from "../lib/net/protobuf/jhaoproto.js";

export class SocketClient {

    public static onSocketRespMsg(baseMsg: jhaoproto.JhaoProto.BaseMsg) {
        if (baseMsg.cmdIndex == jhaoproto.JhaoProto.CmdIndex.None) {
            if (baseMsg.errorCode != jhaoproto.JhaoProto.ErrorCodeWs.Suc) {
                if (baseMsg.errorCode == ErrorCode.Code.LoginError_MultiLogin
                    || baseMsg.errorCode == ErrorCode.Code.Server_Weihu) {
                    GameSocket.instance.closeSocket(false)
                    UIHelper.showCheck(baseMsg.errorMsg, () => {
                        GameController.instance.switchAccount()
                    })
                }
                else if (baseMsg.errorCode == ErrorCode.Code.LoginError_token) {
                    GameController.instance.gotoLogin()
                }
                else {
                    UIHelper.showTip(baseMsg.errorMsg)
                }
            }
        } else {
            // ClubSocketClient.onSocketRespMsg(baseMsg)
        }
        EventManager.instance.dispatch(EventConfig.Event_Socket_msg, baseMsg)
    }


    public static ping() {
        if (UserModel.instance.isLogin) {
            let send_data: jhaoproto.JhaoProto.IReqPing = {
                version: VersionManager.instance.nowVersion
            }
            let send_obj: WsSendData = {
                cmd_index: jhaoproto.JhaoProto.CmdIndex.Ping,
                "en_proto_ctor": jhaoproto.JhaoProto.ReqPing,
                "data": send_data,
                "callback": (respData: jhaoproto.JhaoProto.BaseMsg) => {
                    // Logger.log("ping resp==data=", respData)
                    if (respData.errorCode == jhaoproto.JhaoProto.ErrorCodeWs.Suc) {
                        let respProto: jhaoproto.JhaoProto.RespPing = ProtoManager.instance.decode(jhaoproto.JhaoProto.RespPing, respData.data)
                        ServerTimer.setServerTime(respProto.t);
                    } else {
                        UIHelper.showTip(respData.errorMsg)
                    }
                }
            }
            GameSocket.instance.sendToSocket(send_obj)
        }
    }

    public static login(needGotoHall: boolean, callback: Function = null) {
        if (!UserModel.instance.isLogining && !UserModel.instance.isLogin) {
            UIManager.instance.showLoadingIcon()
            let send_data: jhaoproto.JhaoProto.IReqLogin = {
                account: AccountModel.instance.account,
                token: AccountModel.instance.token,
                nickname: AccountModel.instance.nickname,
                "sex": AccountModel.instance.sex,
                "headimgurl": AccountModel.instance.headimgurl
            }
            // Logger.log("login req==send_data=", needGotoHall, send_data)
            let send_obj: WsSendData = {
                cmd_index: jhaoproto.JhaoProto.CmdIndex.Login,
                "en_proto_ctor": jhaoproto.JhaoProto.ReqLogin,
                "data": send_data,
                "callback": (respData: jhaoproto.JhaoProto.BaseMsg) => {
                    UIManager.instance.hideLoadingIcon()
                    Logger.log("login resp==data=", respData)
                    if (respData.errorCode == jhaoproto.JhaoProto.ErrorCodeWs.Suc) {
                        let respLogin: jhaoproto.JhaoProto.RespLogin = ProtoManager.instance.decode(jhaoproto.JhaoProto.RespLogin, respData.data)
                        UserModel.instance.isLogin = true
                        UserModel.proto2Model(respLogin.userInfo, UserModel.instance)
                        Logger.log("login resp==UserModel=", UserModel.instance)
                        callback && callback()
                        if (needGotoHall) {
                            UIManager.instance.showView(ViewConfig.viewConfig.ViewHall, () => {
                                UIManager.instance.closeButView(ViewConfig.viewConfig.ViewHall)
                            })
                        }
                    } else {
                        UIHelper.showTip(respData.errorMsg)
                    }
                }
            }
            GameSocket.instance.sendToSocket(send_obj)
        }
    }






}